package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SetMealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service(interfaceClass = SetMealService.class)
public class SetMealServiceImp implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;
    //注入freemarker配置对象
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    //注入静态页面输出路径
    @Value("${out_put_path}")
    private String outPutPath;

    //添加套餐和与检查组的关联关系
    @Override
    public void add(Integer[] checkgroupIds, Setmeal setmeal) {
        //添加套餐
        setMealDao.add(setmeal);
        Integer setmealId = setmeal.getId();
        System.out.println("----------" + setmealId);
        //添加关联关系
        this.addAssociationWithCheckGroup(setmealId, checkgroupIds);

        //添加套餐后立刻生成静态页面
        try {
            getStaticPage();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
    }

    //生成套餐列表和详情静态页面
    public void getStaticPage() throws IOException, TemplateException {
        getSetmealListStaticPage();
        getSetmealDetailStaticPage();
    }

    //生成套餐详情静态页面
    public void getSetmealDetailStaticPage() throws IOException, TemplateException {
        //查询数据库得到套餐集合
        List<Setmeal> setmealList = getSetmeal();
        //对套餐集合进行遍历,获取每一个套餐对象,这个套餐对象内部没有进行检查组和检查项信息的封装
        for (Setmeal setmeal : setmealList) {
            //根据套餐对象的id重新查询出套餐对象,这个套餐对象内部封装了检查组合检查项信息,用于套餐的详情展示
            Setmeal setmeal1 = findContentById(setmeal.getId());
            HashMap<Object, Object> map = new HashMap<>();
            map.put("setmeal", setmeal1);
            System.out.println(setmeal);
            genarateStaticPage("mobile_setmeal_detail.ftl", "mobile_setmeal_detail_" + setmeal.getId() + ".html", map);
        }
    }

    //生成套餐列表静态页面
    public void getSetmealListStaticPage() throws IOException, TemplateException {
        //查新套餐集合
        List<Setmeal> setmealList = getSetmeal();
        //通过map集合封装套餐集合
        HashMap<Object, Object> map = new HashMap<>();
        System.out.println(setmealList);
        map.put("setmealList", setmealList);
        //生成静态页面
        genarateStaticPage("mobile_setmeal.ftl", "static_mobile_setmeal.html", map);
    }

    //生成静态页面的通用方法
    public void genarateStaticPage(String templateName, String StaticPageName, HashMap map) throws IOException, TemplateException {
        //获取配置对象
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        //获取模板对象
        Template template = configuration.getTemplate(templateName);
        //获取字符输出流
        FileWriter out = new FileWriter(outPutPath + StaticPageName);
        template.process(map, out);
        out.close();
    }

    //查询套餐分页
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //分页助手进行分页
        PageHelper.startPage(currentPage, pageSize);
        if (queryString == null || queryString == "null") {
            queryString = "";
        }
        Page<Setmeal> page = setMealDao.findByCondition("%" + queryString + "%");
        return new PageResult(page.getTotal(), page.getResult());
    }

    //回显数据
    @Override
    public Setmeal findById(Integer id) {
        Setmeal setmeal = setMealDao.findById(id);
        return setmeal;
    }

    //根据套餐id查找关联的检查组id
    @Override
    public List<Integer> findAssociationWithCheckGroupById(Integer setmealId) {
        List<Integer> list = setMealDao.findAssociationWithCheckGroupById(setmealId);
        return list;
    }

    //编辑套餐及检查组的关联关系
    @Override
    public void edit(Integer[] checkgroupIds, Setmeal setmeal) {
        //更新套餐表内容
        setMealDao.update(setmeal);
        //删除套餐与检查组的关联关系
        setMealDao.deleteAssociationWithCheckGroup(setmeal.getId());
        //添加套餐与检查组的关联关系
        this.addAssociationWithCheckGroup(setmeal.getId(), checkgroupIds);

        //编辑套餐后立刻生成静态页面
        try {
            getStaticPage();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
    }

    //查找所有套餐
    @Override
    public List<Setmeal> getSetmeal() {
        List<Setmeal> list = setMealDao.findAll();
        return list;
    }

    //定义一个方法用于添加与检查组的关联关系
    public void addAssociationWithCheckGroup(Integer setmealId, Integer[] checkgroupIds) {
        for (Integer checkgroupId : checkgroupIds) {
            HashMap<Object, Object> map = new HashMap<>();
            map.put("setmealId", setmealId);
            map.put("checkgroupId", checkgroupId);
            setMealDao.addAssociationWithCheckGroup(map);
        }
    }

    //通过id查找套餐的内容以及包含的检查组和检查项
    @Override
    public Setmeal findContentById(Integer id) {
        Setmeal setmeal = setMealDao.findContentById(id);
        return setmeal;
    }

    @Override
    public HashMap<String, Object> findOrderCount() {
        ArrayList<String> setmealNames = new ArrayList<>();
        ArrayList<Map<String,Object>> setmealCount = setMealDao.findOrderCount();
        for (Map map : setmealCount) {
            String name = (String)map.get("name");
            setmealNames.add(name);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("setmealNames",setmealNames);
        map.put("setmealCount",setmealCount);
        return map;
    }

}
